Unsupervised Transformations


In [ ]:
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import numpy as np
np.set_printoptions(suppress=True)


digits = load_digits()
X, y = digits.data, digits.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

Principal Component Analysis

0) Import the model


In [ ]:
from sklearn.decomposition import PCA

1) Instantiate the model


In [ ]:
pca = PCA(n_components=2)

2) Fit to training data


In [ ]:
pca.fit(X)

3) Transform to lower-dimensional representation


In [ ]:
print(X.shape)
X_pca = pca.transform(X)
X_pca.shape

Visualize


In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=plt.cm.tab10(y))

Manifold Learning


In [ ]:
from sklearn.manifold import Isomap
isomap = Isomap()

In [ ]:
X_isomap = isomap.fit_transform(X)

In [ ]:
plt.figure()
plt.scatter(X_isomap[:, 0], X_isomap[:, 1], c=plt.cm.tab10(y))

Exercises

Visualize the digits dataset using the TSNE algorithm from the sklearn.manifold module (it runs for a couple of seconds).


In [ ]:
# %load solutions/digits_tsne.py